﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AForge.Genetic;

namespace dto
{
   public class doubleAckleyFct:IFitnessFunction
    {
        double IFitnessFunction.Evaluate(IChromosome chromosome)
        {
            myDoubleArrayChromosome chromosome1 = (myDoubleArrayChromosome)chromosome;
            // divide sum and product function of the whole function f(x) = g(x) - h(x)
            double functionValueGx = 0.0, functionValueHx = 0.0, chromosomeValue = 0.0;
            for (int i = 1; i < chromosome1.Length; ++i)
            {
                chromosomeValue = chromosome1.Value[i];
                functionValueGx += Math.Pow(chromosomeValue, 2);
                functionValueHx *= Math.Cos(Math.Abs(2 * Math.PI * chromosomeValue));
            }

            functionValueGx = -0.2 * Math.Sqrt(functionValueGx / chromosome1.Length);
            functionValueHx /= chromosomeValue;

            return (1/((20.0 + Math.Exp(1) - 20 * Math.Exp(functionValueGx) - Math.Exp(functionValueHx))));
        }
    }
}
